﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using JCore.LoLChat;
using JCore.LoLProfiler_Test;
using Win32Process;
using Menu = WartburgInfo.Menu;

namespace JCore
{
    public partial class frmCore : Form
    {
        private readonly AdBlockCore SpotifyAdBlock = new AdBlockCore();
        private readonly LanBellServer LanServer = new LanBellServer();
        private Thread AdBlock;
        private Thread AdBlockStart;
        private bool spotifyRunning;

        public frmCore(bool showForm = true, int lanServerPort = 0)
        {
            InitializeComponent();
            if (!showForm)
            {
                WindowState = FormWindowState.Minimized;
                Hide();
            }

            if (lanServerPort != 0)
            {
                LanServer.StartListening(lanServerPort);
                lanServer.Checked = true;
            }

            // Spotify AdBlock Initializer
            var spotify = new ProcessInfo("spotify.exe");
            spotify.Started += Process_Started;
            spotify.Terminated += Process_Stopped;
            // ---------------------------

            if (Process.GetProcessesByName("spotify")[0] != null)
            {
                Process_Started(this, EventArgs.Empty);
            }
        }


        private void Process_Started(Object sender, EventArgs e)
        {
            spotifyRunning = true;
            AdBlockStart = new Thread(SpotifyAdBlock.StartAdBlock);
            AdBlockStart.Start();
            AdBlockStart.Join();
        }

        private void Process_Stopped(Object sender, EventArgs e)
        {
            spotifyRunning = false;
            AdBlockStart.Abort();
            AdBlock.Abort();
            SpotifyAdBlock.started = false;
        }

        private void tmrSpotify_Tick(object sender, EventArgs e)
        {
            if (spotifyRunning)
            {
                AdBlock = new Thread(SpotifyAdBlock.AdBlock);
                AdBlock.Start();
                AdBlock.Join();
                LanBellServer.MinimizeFootprint();
            }
        }



        private void showFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            Show();
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            Show();
        }

        private void stopJCoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void guidoInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Menu().ShowDialog();
        }

        private void showLoLChatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // frmLoLProfiler.getInstance().Show();
            new frmGetInfoTest().Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SpotifyAdBlock.SetWindowSizeAndLocation((IntPtr) 0x00130386, (IntPtr) 0x00160350, (IntPtr) 0x00130368);
            SpotifyAdBlock.SetWindowSizeAndLocation((IntPtr) 0x001806E0, (IntPtr) 0x003E06D2, (IntPtr) 0x001406CC);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SpotifyAdBlock.SetWindowSizeAndLocation((IntPtr) 0x000B0388, (IntPtr) 0x000506C4, (IntPtr) 0x000B02BE);
            SpotifyAdBlock.SetWindowSizeAndLocation((IntPtr) 0x0008037E, (IntPtr) 0x00130386, (IntPtr) 0x003E06D2);
        }

        private void frmCore_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Fix problems with ports kept open etc...
            Process.GetCurrentProcess().Kill();
        }

        private void frmCore_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                Hide();
            }
        }

        private void stripIngame_Click(object sender, EventArgs e)
        {
            new frmInGameInfo().Show();
        }

        
    }
}